package com.qumitech.visualnovel.controller.rest;

import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.annotation.RequireWhiteList;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.BaseResponse;
import com.qumitech.visualnovel.pubdef.dto.match.MatchCfgNewDTO;
import com.qumitech.visualnovel.pubdef.dto.match.MatchCfgUpdateDTO;
import com.qumitech.visualnovel.pubdef.dto.match.MatchTerminateDTO;
import com.qumitech.visualnovel.pubdef.dto.match.UserMatchResultBatchNewDTO;
import com.qumitech.visualnovel.service.match.MatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/match")
public class MatchController {
    @Autowired
    private MatchService matchService;

    @RequireWhiteList
    @GetMapping("/cfg")
    public BaseRsp listMatchCfg(@RequestParam(value = "match_id", required = false) String matchId,
                                @RequestParam(value = "match_name", required = false) String matchName,
                                @RequestParam(value = "from_start_time", required = false) String fromStartTime,
                                @RequestParam(value = "to_start_time", required = false) String toStartTime,
                                @RequestParam(value = "from_end_time", required = false) String fromEndTime,
                                @RequestParam(value = "to_end_time", required = false) String toEndTime,
                                @RequestParam(value = "offset", required = false, defaultValue = "0")  int offset,
                                @RequestParam(value = "limit", required = false, defaultValue = "20")  int limit){
        Map<String, Object> reqMap = new HashMap<>();
        if (matchId != null){
            reqMap.put("match_id", matchId);
        }
        if (matchName != null){
            reqMap.put("match_name", matchName);
        }
        if (fromStartTime != null){
            reqMap.put("from_start_time", fromStartTime);
        }
        if (toStartTime != null){
            reqMap.put("to_start_time", toStartTime);
        }
        if (fromEndTime != null){
            reqMap.put("from_end_time", fromEndTime);
        }
        if (toEndTime != null){
            reqMap.put("to_end_time", toEndTime);
        }
        reqMap.put("offset", offset);
        reqMap.put("limit", limit);
        return matchService.listMatchCfg(reqMap);
    }

    @RequireWhiteList
    @PostMapping("/cfg")
    public BaseRsp createMatch(@RequestBody MatchCfgNewDTO newDTO){
        return matchService.addMatchCfg(newDTO);
    }

    @RequireWhiteList
    @PutMapping("/{match_id}/cfg")
    public BaseRsp updateMatch(@PathVariable("match_id") String matchId,
                               @RequestBody MatchCfgUpdateDTO updateDTO){
        return matchService.updateMatchCfg(matchId, updateDTO);
    }

    @RequireWhiteList
    @PostMapping("/{match_id}/termination")
    public BaseRsp terminateMatch(@PathVariable("match_id") String matchId,
                                  @RequestBody MatchTerminateDTO terminateDTO){
        return matchService.terminateMatch(matchId, terminateDTO);
    }

    @RequireWhiteList
    @PostMapping("/{match_id}/result")
    public BaseRsp addMatchResult(@PathVariable("match_id") String matchId,
                                  @RequestBody UserMatchResultBatchNewDTO batchNewDTO){
        return matchService.addMatchResult(matchId, batchNewDTO);
    }

    @RequireWhiteList
    @PostMapping("/{match_id}/result/confirm")
    public BaseRsp confirmMatchResult(@PathVariable("match_id") String matchId){
        return matchService.confirmMatchResult(matchId);
    }

    @RequireWhiteList
    @GetMapping("/{match_id}/result")
    public BaseRsp listMatchResult(@PathVariable("match_id") String matchId){
        return matchService.listUserMatchResult(matchId);
    }

    @RequireWhiteList
    @GetMapping("/template")
    public BaseResponse listMatchTemplate(@RequestParam(value = "template_id", required = false) Long templateId,
                                     @RequestParam(value = "game_id", required = false) String gameId,
                                     @RequestParam(value = "match_name", required = false) String matchName,
                                     @RequestParam(value = "template_name", required = false) String templateName){
        return matchService.listMatchTemplate(templateId, gameId, matchName, templateName);
    }

    @RequireWhiteList
    @PutMapping("/template")
    public BaseResponse updateMatchTemplate(@RequestBody JSONObject templateInfo){
        return matchService.updateMatchTemplate(templateInfo);
    }

    @RequireWhiteList
    @PostMapping("/template")
    public BaseResponse createMatchTemplate(@RequestBody JSONObject templateInfo){
        return matchService.createMatchTemplate(templateInfo);
    }

    @RequireWhiteList
    @DeleteMapping("/{template_id}/template")
    public BaseResponse deleteMatchTemplate(@PathVariable("template_id") Long templateId){
        return matchService.deleteMatchTemplate(templateId);
    }

    @RequireWhiteList
    @GetMapping("/template/options")
    public BaseResponse listMatchTemplateOptions() {
        return matchService.listMatchTemplateOptions();
    }

    @RequireWhiteList
    @GetMapping("/value")
    public BaseRsp list(@RequestParam(value = "game_id") String gameId){
        return matchService.listMatchValue(gameId);
    }
}
